package com.yc.ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.yc.Idao.GoodsIDao;
import com.yc.Idao.LoginIDao;
import com.yc.dao.GoodsDao;
import com.yc.dao.LoginDao;
import com.yc.utils.MailUtils;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class Goods extends Composite {
	private Table table;
	private Text text_name;
	private Text text_price;
	private Text text_num;
	private Text text_3;
	private Label lab_image;
	private Label lab_price;
	GoodsIDao dao = new GoodsDao();
	private byte[] bt;//图片的字节数组
	private Text text_roomId;
	private String g_id;
	private TableItem item;
	
	private Map<String, Object> map = MainUI.map;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Goods(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm = new SashForm(this, SWT.VERTICAL);
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm_1 = new SashForm(composite, SWT.VERTICAL);
		
		Composite composite_2 = new Composite(sashForm_1, SWT.NONE);
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		table = new Table(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				showValues(table);
			}
		});
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(239);
		tblclmnNewColumn.setText("商品编号");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(282);
		tblclmnNewColumn_2.setText("商品名称");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(250);
		tblclmnNewColumn_3.setText("价格");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(182);
		tblclmnNewColumn_4.setText("商品图片");
		
		Composite composite_3 = new Composite(sashForm_1, SWT.NONE);
		
		Button btnNewButton = new Button(composite_3, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.removeAll();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("g_name", text_3.getText().toString().trim());
				showinfo(map);
			}
		});
		btnNewButton.setBounds(428, 12, 98, 30);
		btnNewButton.setText("查询");
		
		text_3 = new Text(composite_3, SWT.BORDER);
		text_3.setBounds(268, 14, 117, 26);
		
		
		Label lblNewLabel_3 = new Label(composite_3, SWT.NONE);
		lblNewLabel_3.setBounds(118, 17, 117, 26);
		lblNewLabel_3.setText("按商品名称查询：");
		
		Button btnNewButton_1 = new Button(composite_3, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showValues(table);
			}
		});
		btnNewButton_1.setBounds(677, 12, 98, 30);
		btnNewButton_1.setText("添加商品");
		sashForm_1.setWeights(new int[] {181, 47});
		
		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setLayout(null);
		
		composite_2.setBackgroundImage( SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面1_1_1.PNG") );
		SWTUtil.imagetSize(composite_2);
		//composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_3.setBackgroundImage( SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面1_1_2.PNG") );
		SWTUtil.imagetSize(composite_3);
		
		composite_1.setBackgroundImage( SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面1-2.PNG") );
		SWTUtil.imagetSize(composite_1);
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setBounds(111, 47, 76, 20);
		lblNewLabel.setText("商品名称");
		
		text_name = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		text_name.setBounds(193, 44, 102, 26);
		
		Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_1.setBounds(111, 114, 76, 20);
		lblNewLabel_1.setText("价格");
		
		text_price = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		text_price.setBounds(193, 111, 73, 26);
		
		Label lblNewLabel_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2.setBounds(111, 177, 76, 20);
		lblNewLabel_2.setText("数量");
		
		text_num = new Text(composite_1, SWT.BORDER);
		text_num.setBounds(193, 177, 102, 26);
		text_num.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {	
				showprice();
			}
		});
		
		text_roomId = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		text_roomId.setBounds(750, 44, 98, 26);
		sashForm.setWeights(new int[] {257, 251});
		showinfo(null);
		R_id();
		
		Button btnNewButton_2 = new Button(composite_1, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				GoodsIDao dao = new GoodsDao();
				String r_id = text_roomId.getText().toString().trim();
				String f_num = text_num.getText().toString().trim();
				String f_price = lab_price.getText().toString().trim();
				if(StringUtil.checkNull(g_id, r_id, f_num, f_price)) {
					StringUtil.showMessageBox(getShell(), "信息不完整，无法购买");
					return ;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map = dao.find(r_id);
				System.out.println(MainUI.map.get("c_cardid").toString());
				System.out.println(map.get("l_id").toString());
				int n = dao.add(MainUI.map.get("c_cardid").toString(), g_id, map.get("l_id").toString(), f_num, f_price);
				System.out.println(n);
				if(n == -1) {
					StringUtil.showMessageBox(getShell(), "购买失败！！！\n(余额不足)");
					return ;
				}
				
				if(n > 0) {
					StringUtil.showMessageBox(getShell(), "购买成功！！！");
					MainUI.getMoney();
				}else {
					SWTUtil.showMessage(getShell(), "购买失败！！！");
					return ;
				}

			}
		});
		btnNewButton_2.setBounds(689, 198, 98, 30);
		btnNewButton_2.setText("购买商品");
		
		btnNewButton.setImage(SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面按钮1.PNG"));
		SWTUtil.imagetSize(btnNewButton);
		btnNewButton_1.setImage(SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面按钮2.PNG"));
		SWTUtil.imagetSize(btnNewButton_1);
		btnNewButton_2.setImage(SWTResourceManager.getImage(Goods.class, "/com/yc/images/餐饮界面主界面按钮3.PNG"));
		SWTUtil.imagetSize(btnNewButton_2);
		
		Label lblNewLabel_4 = new Label(composite_1, SWT.NONE);
		lblNewLabel_4.setBounds(639, 139, 54, 20);
		lblNewLabel_4.setText("总价格:");
		
		Label lblNewLabel_5 = new Label(composite_1, SWT.NONE);
		lblNewLabel_5.setText("商品图片");
		lblNewLabel_5.setBounds(331, 47, 76, 20);
		
		lab_price = new Label(composite_1, SWT.NONE);
		lab_price.setBounds(751, 139, 76, 20);
		
		lab_image = new Label(composite_1, SWT.NONE);
		lab_image.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lab_image.setBounds(425, 47, 147, 167);
		
		Label lblNewLabel_6 = new Label(composite_1, SWT.NONE);
		lblNewLabel_6.setBounds(275, 114, 20, 20);
		lblNewLabel_6.setText("元");
		
		Label lblNewLabel_6_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_6_1.setText("元");
		lblNewLabel_6_1.setBounds(833, 139, 20, 20);
		
		Label lblNewLabel_7 = new Label(composite_1, SWT.NONE);
		lblNewLabel_7.setText("房间号");
		lblNewLabel_7.setBounds(639, 47, 54, 20);
		
	}

	private void showinfo(Map<String, Object> map) {
		
		//查询信息‘
		List<Map<String, Object>> list = dao.find(map);
		TableItem tableItem = null;
		//判空
		if(null == list || list.isEmpty()) {
			SWTUtil.showMessage(getShell(), "查无商品信息");
			return;
		}
		//有信息  循环
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
				StringUtil.toString(m.get("g_id")),
				StringUtil.toString(m.get("g_name")),
				StringUtil.toString(m.get("g_price"))
			});
			bt = ( byte[] )m.get("g_image");
			// 字节数组判空
			if( null != bt) {
				Image image = SWTUtil.imageSize(100, 50, new ByteArrayInputStream(bt));
				tableItem.setImage(3, image);
			}
		}
	}
	/**
	 * 点击修改按钮时候 在文本框中显示 选中表格一行的值
	 * @param table
	 */
	public void showValues(Table table){		
		item = table.getItem(table.getSelectionIndex());
		//控件赋值
		g_id = item.getText(0);
		text_name.setText(item.getText(1));
		text_price.setText(item.getText(2));
		text_num.setText("1");
		showprice();
		//获取封面
		Image image = item.getImage(3);
		if(image != null) {
			lab_image.setImage(image);
			SWTUtil.imageSize(lab_image);
		}else {
			lab_image.setImage(null);
		}
	}
	public void showprice() {
		String p = text_price.getText().toString().trim();
		String n = text_num.getText().toString().trim();
		if(StringUtil.checkNull(p) || StringUtil.checkNull(n)) {
			StringUtil.showMessageBox(getShell(), "价格或数量为空！！！");
			text_num.setText("1");
			return ;
		}else if(MailUtils.isNumber(n)) {
			int price = Integer.parseInt(p);
			int num = Integer.parseInt(n);
			lab_price.setText(price * num +"");
		}else {
			text_num.setText("1");
			return;
		}	
	}
	public void R_id() {
		LoginIDao lg = new LoginDao();
		Map<String, Object> map = lg.R_id(this.map.get("c_cardid").toString());
		text_roomId.setText(map.get("c_temp").toString());
	}
}
